﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;

namespace wpfsetup.Setup.Model
{
    /// <summary>
    /// Interop class for showing message boxes using Win32 MessageBox.
    /// </summary>
    public static class User32
    {
        [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern MessageResult MessageBox(IntPtr hWnd, String text, String caption, uint options);

        [Flags]
        public enum MessageBoxOptions : uint
        {
            OK = 0x000000,
            OKCancel = 0x000001,
            AbortRetryIgnore = 0x000002,
            YesNoCancel = 0x000003,
            YesNo = 0x000004,
            RetryCancel = 0x000005,
            CancelTryContinue = 0x000006,

            IconHand = 0x000010,
            IconQuestion = 0x000020,
            IconExclamation = 0x000030,
            IconAsterisk = 0x000040,
            UserIcon = 0x000080,

            IconWarning = IconExclamation,
            IconError = IconHand,
            IconInformation = IconAsterisk,
            IconStop = IconHand,

            DefButton1 = 0x000000,
            DefButton2 = 0x000100,
            DefButton3 = 0x000200,
            DefButton4 = 0x000300,

            ApplicationModal = 0x000000,
            SystemModal = 0x001000,
            TaskModal = 0x002000,

            Help = 0x004000, //Help Button
            NoFocus = 0x008000,

            SetForeground = 0x010000,
            DefaultDesktopOnly = 0x020000,
            Topmost = 0x040000,
            Right = 0x080000,
            RTLReading = 0x100000,
        }

        public enum MessageBoxResult : uint
        {
            OK = 1,
            Cancel,
            Abort,
            Retry,
            Ignore,
            Yes,
            No,
            Close,
            Help,
            TryAgain,
            Continue,
            Timeout = 32000
        }

        /// <summary>
        /// Returns a uint representation of the MessageBox options passed in the parameters.
        /// </summary>
        public static uint Options(MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultbutton)
        {
            MessageBoxOptions result;

            switch (buttons)
            {
                case MessageButtons.OK:
                    result = MessageBoxOptions.OK;
                    break;
                case MessageButtons.OKCancel:
                    result = MessageBoxOptions.OKCancel;
                    break;
                case MessageButtons.AbortRetryIgnore:
                    result = MessageBoxOptions.AbortRetryIgnore;
                    break;
                case MessageButtons.YesNoCancel:
                    result = MessageBoxOptions.YesNoCancel;
                    break;
                case MessageButtons.YesNo:
                    result = MessageBoxOptions.YesNo;
                    break;
                case MessageButtons.RetryCancel:
                    result = MessageBoxOptions.RetryCancel;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("buttons");
            }

            return (uint) (result | (MessageBoxOptions) icon | (MessageBoxOptions) defaultbutton);
        }
    }
}
